<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Danhsachmenu extends CI_Controller {

	public	function __construct()
	{
		parent::__construct();
		if($this->session->userdata('user_id')){
			$this->load->helper("html");
			$this->load->model("tintucs");
			$this->load->model("tailieus");
			$this->load->model("giaobans");
			$this->load->model("users");
			$this->load->model("lich");
			$this->load->model("log_model");
			date_default_timezone_set('Asia/Ho_Chi_Minh');
		}
		else{
			redirect('login');
		}
	}

	public function index($msg=null,$paramter1=null){
		$data['menu'] = 'user_'.$msg;
		$data['list_tailieu'] = $this->tailieus->listTailieu();
		//$data['list_giaoban'] = $this->giaobans->listGiaoban();
		/////////////
		$data['menu_tin'] = $this->giaobans->listGiaoban();
		$list = array();
		foreach ($data['menu_tin'] as $l){		//sap xep nhom danh sach theo giao ban cha
			if($l->id_type_giaoban != 9)
				$list[$l->cha_giaoban][] = $l;
		}
		
		$data['multi_menu_tin'] = $this->giaobans->multiGiaoban($list,0);						
		////////////
		$data['list_lich'] = $this->lich->listLich();
		$this->load->view('header_block_v1',$data);
		$this->load->view('content_sidebar_block',$data);
		switch ($msg){
			case 'menutin':
				$this->viewDanhsachmenutin();
				break;
			case 'menulich':
				$this->viewDanhsachlich();
				break;
			case 'menutailieu':
				$this->viewDanhsachTailieu();
				break;
			case 'viewaddmenutin':
				$this->viewAddMenutin();
				break;
			case 'vieweditmenutin':
				$this->viewEditMenutin($paramter1);
				break;
			case 'viewaddlich':
				$this->viewAddLich();
				break;
			case 'vieweditlich':
				$this->viewEditLich($paramter1);
				break;
			case 'viewaddtailieu':
				$this->viewAddTailieu();
				break;
			case 'viewedittailieu':
				$this->viewEditTailieu($paramter1);
				break;
			default:
				$this->viewDanhsachmenutin();
				break;
		}
		$this->load->view('footer_block');
	}
	//view
	private function viewDanhsachmenutin(){
		$data['menu_tin'] = $this->giaobans->listGiaoban();
		$this->load->view('danhsachmenu/viewDanhsachMenuTin',$data);
	}
	private function viewDanhsachlich(){
		$data['dslich'] = $this->lich->listLich();
		$this->load->view('danhsachmenu/viewDanhsachLich',$data);
	}
	private function viewDanhsachTailieu(){
		$data['dstailieu'] = $this->tailieus->listTailieu();		
		$this->load->view('danhsachmenu/viewDanhsachTailieu',$data);
	}
	private function viewAddMenutin(){
		$data['menu_tin'] = $this->giaobans->listGiaoban();		
		$this->load->view('danhsachmenu/viewAddMenuTin',$data);
	}
	
	public function viewEditMenutin($id_menu){		
		$data['menu'] = $this->giaobans->getTypeGiaoban($id_menu);		
		$this->load->view('danhsachmenu/viewEditMenuTin',$data);
	}
	private function viewAddLich(){
		$this->load->view('danhsachmenu/viewAddLich');
	}
	private function viewEditLich($id_menu){
		$data['lich'] = $this->lich->getLich($id_menu);
		$this->load->view('danhsachmenu/viewEditLich',$data);
	}
	private function viewAddTailieu(){
		$this->load->view('danhsachmenu/viewAddTailieu');
	}
	private function viewEditTailieu($id_tailieu){
		$data['tailieu'] = $this->tailieus->getTypeTailieu($id_tailieu);
		$this->load->view('danhsachmenu/viewEditTailieu',$data);
	}
	
	
	
	public function addMenuTin(){
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		$this->form_validation->set_rules('menu_giaoban', 'Tên menu tin', 'trim|xss_clean|required|min_length[5]');
		$this->form_validation->set_rules('order_giaoban', 'Vị trí', 'trim|xss_clean|is_natural');
		if ($this->form_validation->run() == FALSE){
			$this->index('viewaddmenutin');
		}
		else{
			$data["menu_name_giaoban"] = $this->input->post("menu_giaoban");
			$data["order_giaoban"] = $this->input->post("order_giaoban");
			$data["cha_giaoban"] = $this->input->post("cha_giaoban");
			$url = site_url('danhsachmenu/menutin');
			if($this->giaobans->addTypeGiaoban($data)){
				//if($this->log_model->saveLog(3,null,null,"Tin ".$name_giaoban." - ".$data["tieude_giaoban"]))
				echo "<script>alert('Thêm thành công');	window.location='".$url."'; </script>";
			}
			else
				echo "<script>alert('Lỗi: Không insert được!');	window.location='".$url."'; </script>";
		}
	}
	public function editMenuTin($id_menu){
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		$this->form_validation->set_rules('menu_giaoban', 'Tên menu tin', 'trim|xss_clean|required|min_length[5]');
		$this->form_validation->set_rules('order_giaoban', 'Vị trí', 'trim|xss_clean|is_natural');
		if ($this->form_validation->run() == FALSE){
			$this->index('vieweditmenutin',$id_menu);
		}
		else{
			$data["menu_name_giaoban"] = $this->input->post("menu_giaoban");
			$data["order_giaoban"] = $this->input->post("order_giaoban");
			$data["cha_giaoban"] = $this->input->post("cha_giaoban");
			$url = site_url('danhsachmenu/menutin');
			if($this->giaobans->editTypeGiaoban($data,$id_menu)){
				//if($this->log_model->saveLog(3,null,null,"Tin ".$name_giaoban." - ".$data["tieude_giaoban"]))
				echo "<script>alert('Sửa thành công');	window.location='".$url."'; </script>";
			}
			else
				echo "<script>alert('Lỗi: Không update được!');	window.location='".$url."'; </script>";
		}
	}
	public function delMenutin($id_menu){
		$url = site_url('danhsachmenu/menutin');
		if($this->giaobans->delTypeGiaoban($id_menu)){
			//if($this->log_model->saveLog(1,null,null,"Tin ".$name_giaoban." - ".$tieude_giaoban))
			echo "<script>alert('Bạn đã xóa thành công');	window.location='".$url."'; </script>";
		}
		else
			echo "<script>alert('Lỗi: Không del được!');	window.location='".$url."'; </script>";
	}
	public function trangchu($id_menu){
		$url = site_url('danhsachmenu/menutin');
		if($this->giaobans->updateTrangchu($id_menu)){
			//if($this->log_model->saveLog(1,null,null,"Tin ".$name_giaoban." - ".$tieude_giaoban))
			echo "<script>alert('Đặt thành công');	window.location='".$url."'; </script>";
		}
		else
			echo "<script>alert('Lỗi: Không update được!');	window.location='".$url."'; </script>";
	}
	
	
	public function addLich(){
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		$this->form_validation->set_rules('menu_lich', 'Tên lịch làm việc', 'trim|xss_clean|required|min_length[5]');
		$this->form_validation->set_rules('order_lich', 'Vị trí', 'trim|xss_clean|is_natural');
		if ($this->form_validation->run() == FALSE){
			$this->index('viewaddlich');
		}
		else{
			$data["ten_lich"] = $this->input->post("menu_lich");
			$data["order_lich"] = $this->input->post("order_lich");
			$url = site_url('danhsachmenu/menulich');
			if($this->lich->them_danhsach_lich($data)){
				//if($this->log_model->saveLog(3,null,null,"Tin ".$name_giaoban." - ".$data["tieude_giaoban"]))
				echo "<script>alert('Thêm thành công');	window.location='".$url."'; </script>";
			}
			else
				echo "<script>alert('Lỗi: Không insert được!');	window.location='".$url."'; </script>";
		}
	}
	public function editLich($id_lich){
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		$this->form_validation->set_rules('menu_lich', 'Tên lịch làm việc', 'trim|xss_clean|required|min_length[5]');
		$this->form_validation->set_rules('order_lich', 'Vị trí', 'trim|xss_clean|is_natural');
		if ($this->form_validation->run() == FALSE){
			$this->index('vieweditlich',$id_lich);
		}
		else{
			$data["ten_lich"] = $this->input->post("menu_lich");
			$data["order_lich"] = $this->input->post("order_lich");
			$url = site_url('danhsachmenu/menulich');
			if($this->lich->sua_danhsach_lich($data,$id_lich)){
				//if($this->log_model->saveLog(3,null,null,"Tin ".$name_giaoban." - ".$data["tieude_giaoban"]))
				echo "<script>alert('Sửa thành công');	window.location='".$url."'; </script>";
			}
			else
				echo "<script>alert('Lỗi: Không update được!');	window.location='".$url."'; </script>";
		}
	}
	public function delLich($id_lich){
		$url = site_url('danhsachmenu/menulich');
		if($this->lich->del_danhsach_lich($id_lich)){
			//if($this->log_model->saveLog(1,null,null,"Tin ".$name_giaoban." - ".$tieude_giaoban))
			echo "<script>alert('Bạn đã xóa thành công');	window.location='".$url."'; </script>";
		}
		else
			echo "<script>alert('Lỗi: Không del được!');	window.location='".$url."'; </script>";
	}
	
	
	public function addTailieu(){
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		$this->form_validation->set_rules('loai_tailieu', 'Loại tài liệu', 'trim|xss_clean|required|min_length[5]');
		if ($this->form_validation->run() == FALSE){
			$this->index('viewaddtailieu');
		}
		else{
			$data["ten_type_tailieucol_vtv4"] = $this->input->post("loai_tailieu");
			$url = site_url('danhsachmenu/menutailieu');
			if($this->tailieus->them_danhsach_tailieu($data)){
				//if($this->log_model->saveLog(3,null,null,"Tin ".$name_giaoban." - ".$data["tieude_giaoban"]))
				echo "<script>alert('Thêm thành công');	window.location='".$url."'; </script>";
			}
			else
				echo "<script>alert('Lỗi: Không insert được!');	window.location='".$url."'; </script>";
		}
	}
	public function editTailieu($id_type_tailieu){
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		$this->form_validation->set_rules('loai_tailieu', 'Loại tài liệu', 'trim|xss_clean|required|min_length[5]');
		if ($this->form_validation->run() == FALSE){
			$this->index('viewedittailieu',$id_type_tailieu);
		}
		else{
			$data["ten_type_tailieucol_vtv4"] = $this->input->post("loai_tailieu");
			$url = site_url('danhsachmenu/menutailieu');
			if($this->tailieus->sua_danhsach_tailieu($data,$id_type_tailieu)){
				//if($this->log_model->saveLog(3,null,null,"Tin ".$name_giaoban." - ".$data["tieude_giaoban"]))
				echo "<script>alert('Sửa thành công');	window.location='".$url."'; </script>";
			}
			else
				echo "<script>alert('Lỗi: Không update được!');	window.location='".$url."'; </script>";
		}
	}
	public function delTailieu($id_type_tailieu){
		$url = site_url('danhsachmenu/menutailieu');
		if($this->tailieus->del_danhsach_tailieu($id_type_tailieu)){
			//if($this->log_model->saveLog(1,null,null,"Tin ".$name_giaoban." - ".$tieude_giaoban))
			echo "<script>alert('Bạn đã xóa thành công');	window.location='".$url."'; </script>";
		}
		else
			echo "<script>alert('Lỗi: Không del được!');	window.location='".$url."'; </script>";
	}
}